<?php
/**
* @name /www/index.php
* @author Michael Foss <mfoss@clarionsafety.com>
 * @version 2007.07.02
 *
 * This is the login & registration page.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');
ReqLib('/lib/private/User.php');

/**
 * Defines preload page functionality;
 * this happens after DefineParams,
 * but before anything else.
 */
function DefinePreload() {
  switch($_GET['Action'])  {
    case 'Login':
      $userId = User::GetUserIdByUserName($_POST['Username']);
      $user = new User();

      if (!$user->Load($userId))
        //user did not load
        $GLOBALS['LoginFail'] = "Your login attempt failed. Please try again!!";
      else {
        // check password go to account if true
        $hashPassword = sha1($_POST['Password']);
        if ($user->_PasswordHash  == $hashPassword) {
          $_SESSION['userId'] = $userId;
          if ($_GET['pagereq'])
            header('location:'._webroot($_GET['pagereq']));
          else
            header('location:'._webroot('/account.php'));
          die();
        }
        else
          $GLOBALS['LoginFail'] = "Your login attempt failed. Please try again!";
      }
      break;

    case 'Register':
      $user = new User();
      if ($_POST['Password'] == $_POST['PasswordRetype']  && $_POST['Password'] != '') {
      //passwords match

        if (!$user->Create($_POST['UsernameReg'], $_POST['Email'], $_POST['Password']))
          //creation of the user failed
          $GLOBALS['RegPassFail'] = "Your attempt at registering failed. Your Username is already in use.";
        else {
          $user->FirstName = $_POST['FirstName'];
          $user->LastName = $_POST['LastName'];
          $user->Email = $_POST['Email'];
          $user->PhoneNumber = $_POST['Phone'];
          $user->Street1 = $_POST['StreetOne'];
          $user->Street2 = $_POST['StreetTwo'];
          $user->Street3 = $_POST['StreetThree'];
          $user->City = $_POST['City'];
          $user->State = $_POST['State'];
          $user->Country = $_POST['Country'];
          $user->PostalCode = $_POST['PostalCode'];
          $user->NewsletterSubscription = $_POST['newsletter'];
          $user->Notes = $_POST['Notes'];
          $user->Save();
          $_SESSION['userId'] = $user->UserId;

          if ($_POST['DonorYes'] == 1)
            $user->GrantRole("donator");
          if ($_POST['TravelerYes'] == 1)
            $user->GrantRole("traveler");
          if ($_POST['VolunteerYes'] == 1)
            $user->GrantRole("volunteer");
          $user->Save();
          //creation of user worked

          header('location:'._webroot('/account.php'));
          die();
        }

      }
      else
        //passwords do not match
        $GLOBALS['RegPassFail'] = "Your attempt at registering failed. Your passwords do not match.";
      break;
  }
}

/**
 * Defines page parameters.
 *
 * @param PageParams $p
 */
function DefineParams(&$p) {
  // Define page parameters here.
}

/**
 * Defines custom header content.
 *
 */
function DefineHeader() {
  // Custom HTML info goes here,
  // just before the closing </head> tag.
?><script type="text/javascript">
  $(function() {
    $('#Username').focus();
    $('#Register')
      .button({
        label: 'Register'
      })
      .click(function() {
        $('form[name=Register]').submit();
      });
    $('#Login')
      .button({
        label: 'Login'
      })
      .click(function() {
        $('form[name=Login]').submit();
      });
  });
</script>
<?
}

/**
 * Defines page content.
 *
 */
function DefineContent() {
  // Page content goes here.
  ?>


<center>
<? if($_GET['Action'] == 'Login') echo $GLOBALS['LoginFail'] ?>
<br />
<form name="Login" action="<?=_webroot('/login_register.php?Action=Login' . ($_GET['pagereq'] ? '&pagereq=' . $_GET['pagereq'] : '')) ?>" method="POST">
<table>
	<tr><td colspan="2" align="center"><h1>Login Here</h1></td></tr>
	<tr><td>Username:</td><td><input type = "Text" name="Username" id="Username" value = "<?php echo $_POST['Username'];?>" / ></td></tr>
	<tr><td>Password:</td><td><input type ="password" name="Password" id="Password" /></td></tr>
	<tr><td colspan="2" align="right"><input type="submit" id="Login" /></td></tr>
	<tr><td align="center"><a href="forgotUserName.php">Forgot User Name?</a></td><td align="center"><a href="forgotPassword.php">Forgot Password?</a></td></tr>
</table>
</form>
<hr />

<? if($_GET['Action'] == 'Register') echo $GLOBALS['RegPassFail'] ?>
<br />
<form name="Register" action="<?=_webroot('/login_register.php?Action=Register' . ($_GET['pagereq'] ? '&pagereq=' . $_GET['pagereq'] : '')) ?>"  method="POST">
<table>
<tr >
	<td colspan = 2 align="center"><h1>Register Here</h1></td>
</tr>
<tr>
	<td>Username:</td>
	<td><input type = "Text" name="UsernameReg" value = "<?php echo $_POST['UsernameReg'];?>" / ></td>
</tr>
<tr>
	<td>Password:</td>
	<td><input type ="password" name="Password" /></td>
</tr>
<tr>
	<td>Retype Password:</td>
	<td><input type="password" name="PasswordRetype" /></td>
</tr>
<tr>
	<td>E-mail:</td>
	<td><input type = "Text" name="Email" value= "<?php echo $_POST['Email'];?>" /></td>
</tr>
<tr>
	<td>First Name:</td>
	<td><input type = "Text" name ="FirstName" value = "<?php echo $_POST['FirstName'];?>" /></td>
</tr>
<tr>
	<td>Last Name:</td>
	<td><input type = "Text" name="LastName" value= "<?php echo $_POST['LastName'];?>" / ></td>
</tr>
<tr>
	<td>Street One: </td>
	<td><input type = "Text" name ="StreetOne" value="<?php echo $_POST['StreetOne'];?>" / ></td>
</tr>
<tr>
	<td>Street Two: </td>
	<td><input type = "Text" name ="StreetTwo" value="<?php echo $_POST['StreetTwo'];?>" / ></td>
</tr>
<tr>
	<td>Street Three: </td>
	<td><input type = "Text" name ="StreetThree" value="<?php echo $_POST['StreetThree'];?>" / ></td>
</tr>
<tr>
	<td>City: </td>
	<td><input type = "Text" name ="City" value="<?php echo $_POST['City'];?>" / ></td>
</tr>
<tr>
	<td>State or Province: </td>
	<td><input type = "Text" name ="State" value="<?php echo $_POST['State'];?>" / ></td>
</tr>
<tr>
	<td>Country: </td>
	<td><input type = "Text" name ="Country" value="<?php echo $_POST['Country'];?>" / ></td>
</tr>
<tr>
	<td>PostalCode: </td>
	<td><input type = "Text" name ="PostalCode" value="<?php echo $_POST['PostalCode'];?>" / ></td>
</tr>
<tr>
	<td>Phone Number: </td>
	<td><input type ="Text" name="Phone" value ="<?php echo $_POST['Phone'];?>" /> </td>
</tr>
<tr>
	<td>Would you like to be a Donor?: </td>
	<td><input type="CHECKBOX" name ="DonorYes" value="1" <?=($_POST['DonorYes'] ? 'checked="checked"' : '') ?> /></td>
</tr>
<tr>
	<td>Would you like to be a Volunteer?: </td>
	<td><input type="CHECKBOX" name ="VolunteerYes" value="1" <?=($_POST['VolunteerYes'] ? 'checked="checked"' : '') ?> /> </td>
</tr>
<tr>
	<td>Would you like to be a Traveler?:</td>
	<td><input type="CHECKBOX" name ="TravelerYes" value="1" <?=($_POST['TravelerYes'] ? 'checked="checked"' : '') ?> /></td>
</tr>
<tr>
	<td>Do you want to receive newsletters? </td>
	<td><input type="CHECKBOX" name="newsletter" value="1" <?=($_POST['newsletter'] ? 'checked="checked"' : '') ?> /></td>
</tr>
<tr>
	<td>Anything else we should know?</td>
	<td><textarea name="Notes"><?=htmlspecialchars($_POST['Notes']) ?></textarea></td>
</tr>
<tr><td colspan="2" align="right"><input type="submit" id="Register" /></td></tr>
</table>
</form>
</center>

<?

}

// Launches the page; without this, nothing happens.
require_once('_loader.php');